import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.*;
import com.baomidou.mybatisplus.generator.config.builder.ConfigBuilder;
import com.baomidou.mybatisplus.generator.config.rules.DateType;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;

import java.util.Map;

/**
 * MyBatis-Plus自动生成代码
 * @since 2022-09-07
 * @author LiuChengLong
 */
public class AutoGenerateCode {

    public static void main(String[] args) {
        String username = "LiuChengLong";
        String moduleName = "operate";
        String[] tableNameList = {"tb_operate_info"};
        autoGenerateCode(username, moduleName, tableNameList);
    }

    public static void autoGenerateCode(String username, String moduleName, String... tableNameList) {
        // 构建代码自动生成器 对象
        AutoGenerator autoGenerator = new AutoGenerator();
        // 全局配置
        GlobalConfig globalConfig = new GlobalConfig();
        String property = System.getProperty("user.dir");
        // 配置输出路径
        globalConfig.setOutputDir(property + "/src/main/java");
        globalConfig.setAuthor(username);
        globalConfig.setOpen(false);
        // 是否覆盖
        globalConfig.setFileOverride(true);
        globalConfig.setIdType(IdType.AUTO);
        globalConfig.setDateType(DateType.ONLY_DATE);
        autoGenerator.setGlobalConfig(globalConfig);
        // 配置数据源
        DataSourceConfig dataSourceConfig = new DataSourceConfig();
        dataSourceConfig.setDriverName("com.mysql.cj.jdbc.Driver");
        dataSourceConfig.setUrl("jdbc:mysql://localhost:3306/wordgame?serverTimezone=GMT%2b8");
        dataSourceConfig.setDbType(DbType.MYSQL);
        dataSourceConfig.setUsername("root");
        dataSourceConfig.setPassword("123456");
        autoGenerator.setDataSource(dataSourceConfig);
        // 配置包
        PackageConfig packageConfig = new PackageConfig();
        packageConfig.setModuleName(moduleName);
        packageConfig.setParent("com.liu");
        packageConfig.setController("controller");
        packageConfig.setService("service");
        packageConfig.setServiceImpl("service.impl");
        packageConfig.setMapper("mapper");
        packageConfig.setEntity("entity.base");
        // 配置xml文件生成路径
        ConfigBuilder configBuilder = new ConfigBuilder(packageConfig, dataSourceConfig, null, null, globalConfig);
        Map<String, String> pathInfo = configBuilder.getPathInfo();
        pathInfo.put(ConstVal.XML_PATH, property + "/src/main/resources/com/liu/mapper/" + packageConfig.getModuleName());
        packageConfig.setPathInfo(pathInfo);
        autoGenerator.setPackageInfo(packageConfig);
        // 配置策略
        StrategyConfig strategyConfig = new StrategyConfig();
        // 设置要生成类的表名
        strategyConfig.setInclude(tableNameList);
        strategyConfig.setTablePrefix("tb_");
        strategyConfig.setNaming(NamingStrategy.underline_to_camel);
        strategyConfig.setColumnNaming(NamingStrategy.underline_to_camel);
        // 自动使用lombok
        strategyConfig.setEntityLombokModel(true);
        // controller使用rest形式api
        strategyConfig.setRestControllerStyle(true);
        // 配置自动填充
        /*TableFill createTime = new TableFill("create_time", FieldFill.INSERT);
        TableFill updateTime = new TableFill("update_time", FieldFill.INSERT_UPDATE);
        ArrayList<TableFill> tableFills = new ArrayList<>();
        tableFills.add(createTime);
        tableFills.add(updateTime);
        strategyConfig.setTableFillList(tableFills);*/
        autoGenerator.setStrategy(strategyConfig);

        // 执行
        autoGenerator.execute();
    }
}
